package com.sgb.packers.view.activity;

import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.link.router.PathTeam;
import com.sgb.packers.R;
import com.sgb.packers.databinding.PackersActivityStatisticsBinding;
import com.sgb.packers.entity.WorkStatisticsEntity;
import com.sgb.packers.view.adapter.WorkStatisticsAdapter;
import com.sgb.packers.viewmodel.StatisticsViewModel;
import com.swgk.core.common.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王彬臣
 * @date 2021/11/25
 * @Description 记工统计
 */
@Route(path = PathTeam.WorkStatisticsActivity)
public class WorkStatisticsActivity extends BaseActivity<StatisticsViewModel, PackersActivityStatisticsBinding> {

    private final List<WorkStatisticsEntity> workStatisticsEntityList = new ArrayList<>();
    private WorkStatisticsAdapter statisticsAdapter;

    @Autowired(name = "projectName", desc = "项目名称")
    public String projectName;

    @Autowired(name = "projectNo", desc = "项目的no")
    public String projectNo;

    @Override
    protected int layoutId() {
        return R.layout.packers_activity_statistics;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(StatisticsViewModel.class);

    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        mBinding.actionBar.setOnBackClickListener(this);
        mBinding.refreshLayout.setEnableRefresh(true);
        mBinding.refreshLayout.setEnableLoadMore(true);
        statisticsAdapter = new WorkStatisticsAdapter(workStatisticsEntityList);
        DividerDecoration.builder(this)
                .color(ContextCompat.getColor(WorkStatisticsActivity.this, R.color.color_f5f5f5))
                .size(10, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(mBinding.rvList);
        statisticsAdapter.bindToRecyclerView(mBinding.rvList);
        statisticsAdapter.setEmptyView(R.layout.packers_empty_views);
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadData(true);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData(false);
            }
        });
        initObserve();
        loadData(false);

    }

    private void loadData(boolean isLoadMore) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("dimension", "1");
        objectMap.put("month", "");
//        objectMap.put("projectNo", projectNo);
        objectMap.put("recordType", "1");
        objectMap.put("year", "");
        mViewModel.getWorkStatistics(objectMap, isLoadMore);

    }

    protected void initObserve() {
        mViewModel.workStatisticsEntity.observe(this, listListResponse -> {
            mBinding.refreshLayout.finishRefresh();
            mBinding.refreshLayout.finishLoadMore();
            if (mViewModel.pageNum == 1) {
                workStatisticsEntityList.clear();
            }
            if (listListResponse.getList() != null) {
                workStatisticsEntityList.addAll(listListResponse.getList());
            }
            statisticsAdapter.notifyDataSetChanged();
            if (listListResponse.getCurrentPage() < listListResponse.getTotalPage()) {
                mBinding.refreshLayout.resetNoMoreData();
            } else {
                mBinding.refreshLayout.finishLoadMoreWithNoMoreData();
            }
        });
    }

    @Override
    protected void onMultiClick(View v) {
        if (v.getId() == com.sgb.link.R.id.iv_back_red) {
            finish();
        }
    }
}
